package web

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-iot-project/pkg/app"
	"go-iot-project/pkg/config"
)

type Engine struct {
	*gin.Engine
}

func CreateEngine() *Engine {
	if config.Engine.App.Mode == app.ModelRelease {
		gin.SetMode(gin.ReleaseMode)
	}
	webApiApp := gin.New()
	webApiApp.Use(gin.Recovery())

	return &Engine{webApiApp}
}

func (app *Engine) Serve() {
	port := fmt.Sprintf(":%d", config.Engine.App.Port)
	err := app.Run(port)
	if err != nil {
		panic(fmt.Sprintf("http 服务启动失败, %s", err))
	}
}
